package com.yunhoo.base.db.impl;

import com.yunhoo.base.model.Person;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.RemoteSeverParam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonImpl {

    public static Connection dbConn() {
        RemoteSeverParam param = new RemoteSeverParam("perId");
        String dbServer = param.getDbServer();
        String dbName = param.getDbName();
        String uid = param.getUid();
        String pwd = param.getPwd();

        String connParam = "jdbc:sqlserver://" + dbServer + ":1433;DatabaseName=" + dbName;
        Connection conn = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(connParam, uid, pwd);
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        return conn;
    }

    public static List<Person> queryPerIdReferList() {
        List<Person> perli;
        try {
            Connection conn = dbConn();

            Statement stmt = conn.createStatement();

            String sqlStr = "select infoid cpsn_number,nameid cpsn_name from Tab_user";

            ResultSet rst = stmt.executeQuery(sqlStr);

            perli = new ArrayList<Person>();
            while (rst.next()) {
                Person per = new Person();
                per.setCpsn_number(rst.getString(1));
                per.setCpsn_name(rst.getString(2));
                perli.add(per);
            }
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
            return null;
        }
        return perli;
    }

    public static Person queryByPerNumber(String str) {
        Map map = new HashMap();
        map.put("cpsn_number", str);
        map.put("orgCode", LoginParam.getOrg().getCode());
        return (Person) IbatisTemplate.query("person.queryByPerNumber", map);
    }

    public static void usrUpdatePwd(Map map) {
        IbatisTemplate.update("person.usrUpdatePwd", map);
    }

    public static void main(String[] str) {
        List<Person> perli = queryPerIdReferList();
        System.out.println(" * * * " + "perLi : " + perli.size() + "  * * * ");
        for (Person per : perli) {
            System.out.println("cpsn : " + per.getCpsn_number() + " -- " + per.getCpsn_name());
        }
    }
}
